package edu.xidian.bdilab.jrbigdata.service.enterprise.infoQuery.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseInfo;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseInfoMapper;
import edu.xidian.bdilab.jrbigdata.service.enterprise.infoQuery.InfoQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

@Service
public class InfoQueryServiceImpl implements InfoQueryService {


    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;

    @Override
    public ResponseResult getAllEnterpriseInfo(int pageNum,
                                               int pageSize,
                                               String socialCode,
                                               String enterpriseName,
                                               String ownerName,
                                               String street,
                                               String ownerPhone,
                                               String contactName,
                                               String contactPhone,
                                               String establishAddress,
                                               String businessScop,
                                               String enterpriseDesc,
                                               Integer registeredCaptial,
                                               Integer region,
                                               Integer fundQuota,
                                               Integer available,
                                               Integer isCreditRate) throws Exception{
        QueryWrapper<EnterpriseInfo> enterpriseInfoQueryWrapper=new QueryWrapper<>();
        PageHelper.startPage(pageNum, pageSize);
        EnterpriseInfo enterpriseInfo;
        ResponseResult responseResult = new ResponseResult();
        enterpriseInfoQueryWrapper.like(socialCode!=null,"social_code",socialCode);
        enterpriseInfoQueryWrapper.like(enterpriseName!=null,"enterprise_name",enterpriseName);
        enterpriseInfoQueryWrapper.like(ownerName!=null,"owner_name",ownerName);
        enterpriseInfoQueryWrapper.like(street!=null,"street",street);
        enterpriseInfoQueryWrapper.like(ownerPhone!=null,"owner_phone",ownerPhone);
        enterpriseInfoQueryWrapper.like(contactName!=null,"contact_name",contactName);
        enterpriseInfoQueryWrapper.like(contactPhone!=null,"contact_phone",contactPhone);
        enterpriseInfoQueryWrapper.like(establishAddress!=null,"establish_address",establishAddress);
        enterpriseInfoQueryWrapper.like(businessScop!=null,"business_scop",businessScop);
        enterpriseInfoQueryWrapper.like(enterpriseDesc!=null,"enterprise_desc",enterpriseDesc);
        enterpriseInfoQueryWrapper.eq(registeredCaptial!=null,"registered_captial",registeredCaptial);
        enterpriseInfoQueryWrapper.eq(region!=null,"region",region);
        enterpriseInfoQueryWrapper.eq(fundQuota!=null,"fund_quota",fundQuota);
        enterpriseInfoQueryWrapper.eq(available!=null,"available",available);
        enterpriseInfoQueryWrapper.eq(isCreditRate!=null,"is_credit_rate",isCreditRate);
        try {
            List<EnterpriseInfo> enterpriseInfoList=enterpriseInfoMapper.selectList(enterpriseInfoQueryWrapper);
            PageInfo pageInfo = new PageInfo<>(enterpriseInfoList);
            Map<String, Object> data = new HashMap<>(3);
            data.put("enterpriseInfo List", enterpriseInfoList);
            data.put("Total Page Num", pageInfo.getPages());
            data.put("Total", pageInfo.getTotal());
            if (enterpriseInfoList.size() == 0) {
                responseResult.setCode(BankCode.CHECK_NONE.getCode());
                responseResult.setMessage(BankCode.CHECK_NONE.getMessage());
                responseResult.setSuccess(TRUE);
            } else {
                responseResult.setData(data);
                responseResult.setCode(BankCode.CHECK_SUCCESS.getCode());
                responseResult.setMessage(BankCode.CHECK_SUCCESS.getMessage());
                responseResult.setSuccess(TRUE);
            }
            return responseResult;
        }catch (Exception e){
            responseResult.setCode(BankCode.CHENCK_FAILED.getCode());
            responseResult.setMessage(BankCode.CHENCK_FAILED.getMessage());
            responseResult.setSuccess(FALSE);
            return responseResult;
        }
    }
}
